package com.lee.boot.head.first.demo.controller;


import com.lee.boot.head.first.demo.entity.UserEntity;
import com.lee.boot.head.first.demo.service.UserEntityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

/**
 * @author : Lee
 * @Date: 2018/6/27 15:18
 * @Description:
 */
@Controller
@RequestMapping(value = "/user")
public class LoginController {
    //    注释掉jpa代码
    @Autowired
    private UserEntityService userEntityService;

    @RequestMapping(value = "/loginPage")
    public String LoginController() {
        return "userPAGE";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(UserEntity user, HttpServletRequest request) {
        String name = user.getName();
        String password = user.getPassword();
        Optional<UserEntity> userEntity = userEntityService.get(name);
//        Optional<UserEntity> userEntity = Optional.empty();


        if (null == userEntity) {
            return "name is null！";
        } else if (!userEntity.get().getPassword().equals(password)) {
            return "password is error!";
        }
        request.getSession().setAttribute("_user_info", name);
        return "index";
    }


}
